package servlet;

import bean.Login;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;

@WebServlet(name = "AddServlet", urlPatterns = "/AddServlet")
public class AddServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String hotelID;
        String RoomID=null;
        String HotelName=null;
        HttpSession session=request.getSession(true);
        Login login=null;
        try {
            login= (Login) session.getAttribute("ad_login");
        }catch (Exception e){}
        hotelID=login.getLoginID();

        try {
            Class.forName("org.mariadb.jdbc.Driver");
            Connection conn= DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system", "root", "123456");
            String hotels="Select HotelName from hotels where HotelID='"+hotelID+"'";
            Statement sta=conn.createStatement();
            ResultSet rs=sta.executeQuery(hotels);
            rs.beforeFirst();
            while (rs.next()){
            HotelName=rs.getString("HotelName");}
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        String RoomName=request.getParameter("RoomName");
        String RoomType=request.getParameter("RoomType");
        String RoomSize=request.getParameter("RoomSize");
        String Price=request.getParameter("Price");
        String RoomCondi=request.getParameter("RoomCondi");
        if (RoomCondi.equals("未预定")){
            RoomCondi="1";

        }else if (RoomCondi.equals("已预定")) {
            RoomCondi="0";
        }
        try {
            Class.forName("org.mariadb.jdbc.Driver");
            Connection conn= DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system", "root", "123456");
            String order="Select RoomID from rooms";
            Statement sta=conn.createStatement();
            ResultSet rs=sta.executeQuery(order);
            rs.beforeFirst();
            ArrayList<Long> check=new ArrayList<>();
            while (rs.next()){
                String user=rs.getString("RoomID");
                Long UserID=Long.parseLong(user);
                check.add(UserID);
            }
            while (true) {
                String id_string = "";   //  预定义字符串准备拼接
                for (int j = 0; j < 10; j++) {
                    if (j == 0) {
                        //  保证第一位不为0
                        id_string += (long) (Math.random() * 9 + 1) + "";
                    } else {
                        //  随机生成0~9 、拼接
                        id_string += (long) (Math.random() * 10) + "";
                    }
                }
                long uid = Long.parseLong(id_string);   //  转为long类型，int类型可能会超出最大值异常
                boolean flag = true;    //  定义一面旗帜做为检验是否重复值标记
                for (Long c : check) {
                    if (c == uid) {     //  遍历判断，若有重复值，则flag=false，并打破循环
                        flag = false;
                        break;
                    }

                }
                if (flag) {     //  若flag为true，则证明没有重复值
                    RoomID=""+uid+"";
                    break;
                }
            }
            System.out.println("AddServlet97行"+HotelName+RoomID+RoomCondi+RoomName+RoomType+RoomSize);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String sql="insert into rooms(HotelID,RoomID,RoomName,RoomType,RoomSize,Price,RoomCondi) values(?,?,?,?,?,?,?)";
        try {
            Class.forName("org.mariadb.jdbc.Driver");
            Connection conn= DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system","root","123456");
            PreparedStatement statement=conn.prepareStatement(sql);
            statement.setString(1,HotelName);
            statement.setString(2,RoomID);
            statement.setString(3,RoomName);
            statement.setString(4,RoomType);
            statement.setString(5,RoomSize);
            statement.setString(6,Price);
            statement.setString(7,RoomCondi);
            statement.executeUpdate();
            conn.close();

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect("admin-equry.jsp");
    }

}
